import * as THREE from "three";
import { config } from './config.js';
import { productData } from './data.js';
import { createProduct , createProducts , getStoryByunitNum , getEnclosure} from "../libs/common.js";
import { CreateTag } from '../libs/tag.js';

import { createText  } from '../libs/createText.js';
import $ from 'jquery';

var sceneGroup = new THREE.Group();
var picks = [];
var textureLoader = new THREE.TextureLoader();




// 绘制库房大体坐标
const gt = textureLoader.load( "./src/assets/textures/grasslight-big.jpg" );
gt.wrapS = THREE.RepeatWrapping;
gt.wrapT = THREE.RepeatWrapping;
gt.repeat.x = gt.repeat.y = 10;
const floorGeometry = new THREE.PlaneBufferGeometry( config.building.sX, config.building.sY, 32 );
// color: "rgb(213,194,160)"
const floorMaterial = new THREE.MeshLambertMaterial( {map:gt} );
const floor = new THREE.Mesh( floorGeometry, floorMaterial );
floor.position.y = -0.3;
floor.rotation.x = -Math.PI / 2;
floor.scale.set(1.1,1.1,1);

// 道路

var roadTxture = textureLoader.load("./src/assets/images/road.png");
roadTxture.wrapS = THREE.RepeatWrapping;
roadTxture.wrapT = THREE.RepeatWrapping;
roadTxture.repeat.x = 1;
const roadGeometry = new THREE.PlaneBufferGeometry( config.building.sY , 16 , 32 );
const roadMaterial = new THREE.MeshLambertMaterial( {map:roadTxture} );
const road = new THREE.Mesh( roadGeometry, roadMaterial );
road.position.x =  - 12;
road.rotation.x = -Math.PI / 2;
road.rotation.z = -Math.PI / 2;
road.scale.set(1.1,1.1,1);
sceneGroup.add(road);

// 根据厂房位置获取初始定位坐标
// 根据货物属于垛位编号，进行位置偏移  [需要指定 根据不同编号，获取到该货物的位置]
// 仓库里面有多个垛位
// 垛位里面分多少存放货物点 [ 所有垛位的存货点数量是一样的] 
    // 
// 厂区 -》 仓库 -》 垛位

// -》 存货点16层

// 选中效果cube
const t_map = textureLoader.load( './src/assets/images/t_map.png' );

const hot_map = textureLoader.load( './src/assets/images/h_wave.png' );
    hot_map.wrapS = THREE.RepeatWrapping;
    hot_map.wrapT = THREE.RepeatWrapping;
    // hot_map.repeat.y = 20;

const hot_geometry = new THREE.BoxBufferGeometry( config.stack.sX,15, config.stack.sY);
const hot_material = new THREE.MeshBasicMaterial( 
    {
        color: 0x00ffff,
        transparent:true,
        opacity:1,
        depthTest: true,
        map:hot_map,
        // side: THREE.DoubleSide,
    } );
const hot_cube = new THREE.Mesh( hot_geometry, hot_material );
hot_cube.visible = false;
sceneGroup.add(hot_cube);



var url = "http://jvlihd.cn/www/webApi/wisdomStorehouse/api.php";
$.post(url,{},function(stackList){
    window.stackList = stackList
    loadFont(stackList);
},"json");




// 加载厂房牌号文字
function loadFont(stackList) {
    const loader = new THREE.FontLoader();
    var fontName = "gentilis";
    var fontWeight = "bold";
    loader.load( './src/assets/fonts/' + fontName + '_' + fontWeight + '.typeface.json', function ( response ) {

        var parames = {
            font:response,
            size:4,
            height:1,
            bevelThickness:0.1,
            bevelSize:0.3,
            curveSegments:1,
            bevelEnabled : true,
        }
        createStorehouse(parames,stackList);

    } );
}


// 创建库房
function createStorehouse(parames,stackList)
{
    // 地板材质 公用
    const B_floorMaterial = new THREE.MeshLambertMaterial( {color: "rgb(35,37,37)"} );
    //边角线材质  公用 
    const edges_LineBasicMaterial = new THREE.LineBasicMaterial( {
        color: 0xffffff,
        // linewidth: 1,
        // linecap: 'round', //ignored by WebGLRenderer
        // linejoin:  'round' //ignored by WebGLRenderer
    } );

    const material = new THREE.MeshBasicMaterial( {color: 0x00ff00 , transparent:true,opacity:0,depthTest: false} );

    var storehouses = config.building.storehouses;
    storehouses.forEach(element => {
        // 厂房搭建
        const geometry = new THREE.BoxBufferGeometry( element.sx, element.sy, element.sz);
        const cube = new THREE.Mesh( geometry, material );

        var pos    = new THREE.Vector3(  -element.sx / 2   +  config.building.sX / 2  -  element.x,   element.sy / 2,  - element.sz / 2 +  config.building.sY / 2  - element.z);
        cube.position.copy(pos);
        cube.name = element.name;
        sceneGroup.add( cube );

        // 厂房地板
        const floorGeometry = new THREE.PlaneBufferGeometry(  element.sx, element.sz );
        const cube_floor = new THREE.Mesh( floorGeometry, B_floorMaterial );
        cube_floor.rotation.x = -Math.PI / 2;
        cube_floor.scale.set(1.1,1.4,1);
        cube_floor.position.y =  -element.sy / 2 ;
        cube.add(cube_floor);

        if(element.stacks){
            var stacks = element.stacks;
            if(stacks.length > 0)
            {
                var offset = {
                    x : element.sx  / 2, //仓库宽半径
                    y : element.sz  / 2, //仓库长半径
                }
                // 垛位搭建
                stacks.forEach(el => {
            
                    const stack_geometry = new THREE.BoxBufferGeometry( config.stack.sX,element.sy, config.stack.sY);
                
                    const stack_material = new THREE.MeshBasicMaterial( 
                        {
                            color: 0x00ffff,
                            transparent:true,
                            opacity:0.1,
                            depthTest: true,
                            // side: THREE.DoubleSide,
                        } );
                    const stack_cube = new THREE.Mesh( stack_geometry, stack_material );

                    // 垛单位mesh样式
                    // var stack_cube = getEnclosure();

                    var pos = new THREE.Vector3(   
                        -el.x *  (config.stack.sX + config.stack.offset_x) + offset.x  - config.stack.sX_half  , 
                        el.y ,
                        -el.z *  (config.stack.sY + config.stack.offset_y) - config.stack.sY_half   + offset.y);
                    stack_cube.position.copy(pos);
                    stack_cube.name = element.name+"_"+el.name;
                    // console.log( stack_cube.position);

                    cube.add(stack_cube);
                })
            }
        }



        // 厂牌处理
        if(element.isBrand)
        {
            var brand_group = new THREE.Group();
            const brand_geometry = new THREE.BoxBufferGeometry( 10, 5, 2 );
            const brand_material = new THREE.MeshPhongMaterial( {color: 0x00ff00 , transparent:true,opacity:0.4} );
            const brand_cube = new THREE.Mesh( brand_geometry, brand_material );
            var brand_pos = pos.clone();
            var brandNum = parseInt(element.BrandNum); 
            brand_pos.y = 2.5;

            // 边角线美化
            const edges = new THREE.EdgesGeometry( brand_geometry );
            const line = new THREE.LineSegments( edges, edges_LineBasicMaterial );
            brand_cube.add( line );



            if(brandNum < 14){
                brand_cube.rotation.x = Math.PI / 4;
                brand_pos.x -=  element.sx / 2  + 6;
                brand_group.rotation.y = Math.PI / 2;
            }else{
                brand_cube.rotation.x = Math.PI / 4;
                brand_pos.x +=  element.sx / 2  + 6;
                brand_group.rotation.y -= Math.PI / 2 ;
            }
            brand_group.position.copy(brand_pos);
        
            // var axesHelper = new THREE.AxesHelper(10);
            brand_group.add(brand_cube);
            sceneGroup.add( brand_group );
            var text = createText(element.BrandNum,parames);
            // text.add(axesHelper);
            brand_cube.add(text);

        }

    
        
    });

    // console.log(sceneGroup);
    // 垛位批量创建 参数 stackName,geometry,material,productData
    // createProducts 
    const unit_geometry = new THREE.BoxBufferGeometry( 0.95 * config.unit.sX, 0.95 * config.unit.sH, 0.95 * config.unit.sY );
    const unit_material = new THREE.MeshLambertMaterial({emissive :"rgb(100,100,100)"});

    stackList.forEach( storehouse =>{
        // 仓库
        var cNum = storehouse.cNum;
        var statcks = storehouse.statcks;

        // 垛位
        statcks.forEach( statck => {
            var stackCode = statck.stackCode;
            var stackInfos = statck.stackInfos;
            
            if(stackInfos.length > 0)
            {
                var statckProduct =  createProducts(stackCode,unit_geometry,unit_material,stackInfos);
                statckProduct.cName = cNum;
                var statckName = cNum + "_" +stackCode;
                var statckContent =  sceneGroup.getObjectByName(statckName);
                // console.log(statckContent);
                statckContent.add(statckProduct);
                picks.push(statckProduct);
            }
        })
    })

    // productData.forEach( stacks => {
    //     var statckName = stacks.name;
    //     var statckProduct =  createProducts(statckName,unit_geometry,unit_material,stacks.statck);
    //     // statckProduct.castShadow = true;
    //     // statckProduct.receiveShadow = true;




    //     var statckContent =  sceneGroup.getObjectByName(statckName);
    //     statckContent.add(statckProduct);
    //     picks.push(statckProduct);
    //     // console.log(statckName);

    //     // // tag标签
    //     // var length = stacks.statck.length;
    //     // // console.log(statckContent);
    //     // var h = (Math.floor(length / 12) ) * config.unit.sH  - 15 / 2;
    //     // var postion  = statckProduct.position.clone();
    //     // postion.y = h * 0.2;
    //     // var tag = CreateTag();
    //     // tag.position.copy(postion);
    //     // tag.element.innerHTML = Math.floor(Math.random() * 100);
    //     // statckContent.add(tag);
    // })

}

// console.log(sceneGroup);


export { sceneGroup , floor , picks ,  productData  , hot_cube , hot_map};